-- Winter Anathema (c) v.v.balashoff
local Color = require('libs/colorlibrary/colorlibrary')
local Domain = require('leviathan/ECS/Domain')
local DomainSwitcherComponent = require('leviathan/ECS/Core/Components/DomainSwitcherComponent')
local DomainSwitcherSystem = require('leviathan/ECS/Core/Systems/DomainSwitcherSystem')
local Entity = require('leviathan/ECS/Entity')
local i18n = require('libs/i18n')
local Log = require('leviathan/Log')
local MouseClickComponent = require('leviathan/ECS/UI/Components/MouseClickComponent')
local MouseHoverComponent = require('leviathan/ECS/UI/Components/MouseHoverComponent')
local MusicComponent = require('leviathan/ECS/Audio/Components/MusicComponent')
local RectangleComponent = require('leviathan/ECS/UI/Components/RectangleComponent')
local TextComponent = require('leviathan/ECS/UI/Components/TextComponent')
local TextShuffleComponent = require('leviathan/ECS/UI/Components/TextShuffleComponent')
local TimerComponent = require('leviathan/ECS/UI/Components/TimerComponent')
local UIInputSystem = require('leviathan/ECS/UI/Systems/InputSystem')
local UIRenderSystem = require('leviathan/ECS/UI/Systems/RenderSystem')
local UITextShuffleSystem = require('leviathan/ECS/UI/Systems/TextShuffleSystem')
local UITimerSystem = require('leviathan/ECS/UI/Systems/TimerSystem')
local UITweenSystem = require('leviathan/ECS/UI/Systems/TweenSystem')

local Preface = Domain:derive('Preface')

function Preface:new(world)
    -- call parent constructor
    Preface.super.new(self, world, 'Preface')
    -- get screen size
    local screenW, screenH = love.window.getMode()
    -- entity: background
    local bg = Entity(self, 'PrefaceBackground', 0, 0, screenW, screenH)
    bg:setPriority(400)
    -- get managers
    local FontsManager = world:getManager('FontsManager')
    -- background rectangle component
    local bgRectangleComp = RectangleComponent('fill', 0, 0, screenW, screenH, 0, 0, Color.white)
    bg:addComponent(bgRectangleComp)
    -- calculate subtitle width and height limits
    local ys = screenH * 0.3
    local textSize = screenH * 0.05 -- 5% of screen height
    local font = FontsManager:getFont('CourierNewCyrBold', textSize)
    -- entity: subtitle
    local subtitle = Entity(self, 'PrefaceSubtitle', 0, ys, screenW, textSize)
    local subtitleTextComp = TextComponent('presents', 0, 0, screenW, ys, i18n.translate('vvb_presents'), font, Color.black, 'center')
    subtitle:addComponent(subtitleTextComp)
    -- shuffle
    local shuffle = TextShuffleComponent(subtitleTextComp, 20, 1.5)
    subtitle:addComponent(shuffle)
    -- entity: mouse clicker
    local clicker = Entity(self, 'PrefaceClicker', 0, 0, screenW, screenH)
    local mouseHoverComponent = MouseHoverComponent()
    mouseHoverComponent.hovered = true -- to get all mouse clicks
    clicker:addComponent(mouseHoverComponent)
    local mouseClickComponent = MouseClickComponent(self.buttonClick, nil, 'ButtonAsyncPressedComponent')
    clicker:addComponent(mouseClickComponent)
    -- entity: cronSwitcher to PrefaceTitle
    local cronSwitcher1 = Entity(self, 'DomainSwitcher1')
    local ds1 = DomainSwitcherComponent('DomainSwitcherComponent', self, 'PrefaceTitle', true)
    ds1.enabled = false
    cronSwitcher1:addComponent(ds1)
    local t1 = TimerComponent(1.2, ds1, 'enabled', true) -- 1.2 second til switch
    cronSwitcher1:addComponent(t1)
    -- entity: cronSwitcher to PressAnyKey
    local cronSwitcher2 = Entity(self, 'DomainSwitcher2')
    local ds2 = DomainSwitcherComponent('DomainSwitcherComponent', self, 'PressAnyKey', true)
    ds2.enabled = false
    cronSwitcher2:addComponent(ds2)
    local t2 = TimerComponent(20, ds2, 'enabled', true) -- 30 second til switch
    cronSwitcher2:addComponent(t2)
    -- entity: music
    local musicPlayer = Entity(self, 'Music')
    local musicComp = MusicComponent('PrefaceMusic')
    musicPlayer:addComponent(musicComp)
    -- systems
    local switcher = DomainSwitcherSystem(self)
    local render = UIRenderSystem(self)
    local input = UIInputSystem(self)
    local shuffleS = UITextShuffleSystem(self)
    local timer = UITimerSystem(self)
    local tween = UITweenSystem(self)
    -- loading systems
    Preface:addSystem(self, switcher)
    Preface:addSystem(self, render)
    Preface:addSystem(self, input)
    Preface:addSystem(self, shuffleS)
    Preface:addSystem(self, timer)
    Preface:addSystem(self, tween)
end

function Preface:onEnter()
    local audio = world:getDomain('Audio')
    audio:playMusic('PrefaceMusic')
end

function Preface:onExit(to)
    local audio = world:getDomain('Audio')
    if to == 'MainMenu' then
        audio:stopMusic('Preface')
        world:deleteDomain('PrefaceTitle')
        world:deleteDomain('PressAnyKey')
    end
end

function Preface:buttonClick(button)
    Log:debug('[Preface] [Preface] => [MainMenu]')
    world:switchDomain(self, 'MainMenu')
end

return Preface
